<?php

/**
 * This is the model class for table "tb_tenant".
 *
 * The followings are the available columns in table 'tb_tenant':
 * @property integer $id
 * @property string $tenant_name
 * @property string $institutional_name
 * @property string $address
 * @property string $telephone_number_1
 * @property string $telephone_number_2
 * @property string $email
 * @property string $fax_number_1
 * @property string $fax_number_2
 * @property string $flag
 * @property string $admin_user
 * @property integer $educational_stage_id
 * @property string $registration_date
 *
 * The followings are the available model relations:
 * @property EducationalStage $educationalStage
 */
class Tenant extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_tenant';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tenant_name, institutional_name, address, telephone_number_1, email, fax_number_1, admin_user, educational_stage_id', 'required'),
			array('educational_stage_id', 'numerical', 'integerOnly'=>true),
			array('tenant_name, institutional_name', 'length', 'max'=>250),
			array('address', 'length', 'max'=>500),
			array('telephone_number_1, telephone_number_2, email, fax_number_1, fax_number_2', 'length', 'max'=>20),
			array('flag', 'length', 'max'=>9),
			array('admin_user', 'length', 'max'=>50),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, tenant_name, institutional_name, address, telephone_number_1, telephone_number_2, email, fax_number_1, fax_number_2, flag, admin_user, educational_stage_id, registration_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'educationalStage' => array(self::BELONGS_TO, 'EducationalStage', 'educational_stage_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'tenant_name' => 'Tenant Name',
			'institutional_name' => 'Institutional Name',
			'address' => 'Address',
			'telephone_number_1' => 'Telephone Number 1',
			'telephone_number_2' => 'Telephone Number 2',
			'email' => 'Email',
			'fax_number_1' => 'Fax Number 1',
			'fax_number_2' => 'Fax Number 2',
			'flag' => 'Flag',
			'admin_user' => 'Admin User',
			'educational_stage_id' => 'Educational Stage',
			'registration_date' => 'Registration Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('tenant_name',$this->tenant_name,true);
		$criteria->compare('institutional_name',$this->institutional_name,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('telephone_number_1',$this->telephone_number_1,true);
		$criteria->compare('telephone_number_2',$this->telephone_number_2,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('fax_number_1',$this->fax_number_1,true);
		$criteria->compare('fax_number_2',$this->fax_number_2,true);
		$criteria->compare('flag',$this->flag,true);
		$criteria->compare('admin_user',$this->admin_user,true);
		$criteria->compare('educational_stage_id',$this->educational_stage_id);
		$criteria->compare('registration_date',$this->registration_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Tenant the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave(){

		$this->tenant_name = strtoupper($this->tenant_name);
		$this->institutional_name = strtoupper($this->institutional_name);

		return parent::beforeSave();
	}

	public function afterSave(){
		if($this->isNewRecord){
			$newUser = new TenantUser();
			$newUser->user_name = $this->admin_user;
			$newUser->password = md5("admin");
			$newUser->tenant_id = $this->id;
			$newUser->save(); 
		}

		return parent::afterSave();
	}

}
